﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Dimok.Converters {

  public class ResourceToImageSourceConverter : IValueConverter {
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      return ResourceHelper.GetBitmap((parameter+"").Split(';')[1] , (parameter+"").Split(';')[0]);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      throw new NotImplementedException();
    }

    #endregion
  }
  public class PhoneConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      Regex re = new Regex(@"(?<p1>\d{3})\D*(?<p2>\d{3})\D*(?<p3>\d{4})\D*$", RegexOptions.RightToLeft);
      return re.Replace(value + "", "($1) $2-$3");
    }
    public object ConvertBack(object value, Type targetType, object parameter,
                          System.Globalization.CultureInfo culture) {
      return value;
    }
  }
  public class StringFormatter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      if (!(parameter + "").StartsWith("{")) parameter = "{0:" + parameter + "}";
      return string.Format(parameter + "", value);
    }
    public object ConvertBack(object value, Type targetType, object parameter,
                          System.Globalization.CultureInfo culture) {
      return value;
    }
  }
  public class StringParser : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      var p = parameter + "";
      bool doFormat = p.StartsWith("{}");
      if (doFormat) p = p.Substring(2);
      var re = Regex.Split(p, "//");
      if (re.Length > 2) {
        var m = Regex.Match(value + "", re[1] + "");
        value = m.Groups.Count > 0 ? m.Groups[0].Value : m.Value;
        p = re[0] + "0" + re[2];
      }
      return doFormat ? string.Format(p, value) : value;
    }
    public object ConvertBack(object value, Type targetType, object parameter,
                          System.Globalization.CultureInfo culture) {
      return value;
    }
  }
  public class SSNConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      Regex re = new Regex(@"(?<p1>\d{3})\D*(?<p2>\d{2})\D*(?<p3>\d{4})\D*$", RegexOptions.RightToLeft);
      return re.Replace(value + "", "$1-$2-$3");
    }
    public object ConvertBack(object value, Type targetType, object parameter,
                          System.Globalization.CultureInfo culture) {
      return value;
    }
  }
  public class VisibilityConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			bool ret;
			if (value == null) ret = false;
			else if (value is int) ret = (int)value > 0;
			else if (value is bool) ret = (bool)value;
			else if (value is bool?) ret = ((bool?)value).HasValue && ((bool?)value).Value;
			else if (value is string) ret = value + "" != "";
			else throw new Exception("VisibilityConverter does not support " + value.GetType() + " type.");
			return (parameter + "" == "!" ? !ret : ret) ? Visibility.Visible : Visibility.Collapsed;
    }
    public object ConvertBack(object value, Type targetType, object parameter,
                          System.Globalization.CultureInfo culture) {
      return value;
    }
  }
  public class BooleanToDoubleConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      var values = (parameter + "").Split(',');
      return double.Parse((bool)value ? values[1] : values[0]);
    }
    public object ConvertBack(object value, Type targetType, object parameter,
                          System.Globalization.CultureInfo culture) {
      return value;
    }
  }

  public class PropertyViewer : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      return (value ?? new object()).ToDictionary();
    }
    public object ConvertBack(object value, Type targetType, object parameter,
                          System.Globalization.CultureInfo culture) {
      return value;
    }
  }
  static class Types {
    internal static Type[] Whole = new[] { typeof(int), typeof(int?), typeof(byte), typeof(byte?) };
    internal static Type[] Float = new[] { typeof(decimal), typeof(decimal?), typeof(double), typeof(double?) };
    internal static Type[] All = Whole.Union(Float).ToArray();
  }
  public class DefaultFormatConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      if (value + "" == "") return " ";
      int d = 0;
      if (!int.TryParse(parameter + "", out d)) d = 2;
      if (Types.Whole.Contains(value.GetType())) d = 0;
      return Types.All.Contains(value.GetType()) ? string.Format("{0:n" + d + "}", value) : value;
    }
    public object ConvertBack(object value, Type targetType, object parameter,
                          System.Globalization.CultureInfo culture) {
      return value;
    }
  }

  public class EmptySpaceConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
      return value ?? " ";
    }
    public object ConvertBack(object value, Type targetType, object parameter,
                          System.Globalization.CultureInfo culture) {
      return value;
    }
  }
	public class BooleanConverter : IValueConverter {
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			var ret = (bool)(value ?? false);
			return parameter + "" == "!" ? !ret : ret;
		}
		public object ConvertBack(object value, Type targetType, object parameter,
													System.Globalization.CultureInfo culture) {
			return value;
		}
	}
	public class PassThroughConverter : IValueConverter {
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			return value;
		}
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			return value;
		}
	}

  //public class GridLenthBooleanConverter : IValueConverter {
  //  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
  //    var values = (parameter + "").Split(',');
  //    return new DataGridLengthConverter().ConvertFromString((bool)value ? values[1] : values[0]);
  //  }
  //  public object ConvertBack(object value, Type targetType, object parameter,
  //                        System.Globalization.CultureInfo culture) {
  //    return value;
  //  }
  //}
  public class StringToArrayTypeConverter : TypeConverter {
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
      return sourceType.IsAssignableFrom(typeof(string));
    }
    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
      string text = value as string;
      if (text != null) {
        return text.Split(',');
      }
      return null;
    }
  }
  public class StringToListTypeConverter : TypeConverter {
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
      return sourceType.IsAssignableFrom(typeof(string));
    }
    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
      string text = value as string;
      if (text != null) {
        return new List<string>(text.Split(','));
      }
      return new List<string>();
    }
  }

}
